<!-- 福分规则管理 -->
<template>
  <page-header-wrapper>
    <a-card class="table-card">
      <GzSearchForm :searchFunc="searchFunc" :resetFunc="()=>{searchData = {};gzThreeRef.clear()}" searchConditionNum="6">
        <a-form-item class="table-search-item">
          <GzTreeSelect ref="gzThreeRef" v-model:value="searchData['agentNo']" :isNeedDefault="false" />
        </a-form-item>
        <GzTextUp v-model:value="searchData['scoreRuleName']" placeholder="规则名称" />
        <a-form-item class="table-search-item">
          <a-select v-model:value="searchData['state']" placeholder="状态">
            <a-select-option value="">全部</a-select-option>
            <a-select-option :value="0">不启用</a-select-option>
            <a-select-option :value="1">启用</a-select-option>
          </a-select>
        </a-form-item>
      </GzSearchForm>

      <!-- 列表渲染 -->
      <GzTable
        ref="infoTableRef"
        :init-data="true"
        :req-table-data-func="reqTableDataFunc"
        :table-columns="tableColumns"
        :search-data="searchData"
      >
        <template #topBtnSlot>
          <a-button v-if="$access('ENT_SCORE_RULE_INFO_ADD')" type="primary" @click="addOrEditFunc(null)">
            <plus-outlined />新建
          </a-button>
        </template>

        <template #bodyCell="{ column, record }">
          <template v-if="column.key == 'state'">
            <GzTableColState :state="record.state" :show-switch-type="$access('ENT_SCORE_RULE_INFO_EDIT')" :on-change="(state) => { return stateChangeFunc(record.scoreRuleId, state)}" />
          </template>
          <template v-if="column.key === 'overType'">
            <template v-if="record.overType == 0">领取后{{ record.overDays }}天过期</template>
            <template v-else-if="record.overType == 1">{{ dayjs(record.overTime).format('YYYY[年]M[月]D[日]') }}过期</template>
            <template v-else>领取后次季度首月月底过期</template>
          </template>
          <template v-if="column.key === 'operation'">
            <a-button v-if="$access('ENT_SCORE_RULE_INFO_VIEW')" type="link" @click="detailFunc(record.scoreRuleId)">详情</a-button>
            <a-button v-if="$access('ENT_SCORE_RULE_INFO_EDIT')&& record.state==0" type="link" @click="addOrEditFunc(record.scoreRuleId)">修改</a-button>
          </template>
        </template>
      </GzTable>
    </a-card>
    <!-- 新增修改 -->
    <AddOrEdit ref="addOrEditRef" :callbackFunc="searchFunc" />
    <!-- 详情 -->
    <Detail ref="detailRef" />
  </page-header-wrapper>
</template>

<script lang="ts" setup>
import dayjs from 'dayjs'
import AddOrEdit from './AddOrEdit.vue'
import Detail from './Detail.vue'
import { $getScoreRule,$scoreRuleState } from '@/api/score/scoreRule/index'
import { ref, reactive, getCurrentInstance } from 'vue'

const { $infoBox, $access } = getCurrentInstance()!.appContext.config.globalProperties

const gzThreeRef = ref()
const infoTableRef = ref()
const addOrEditRef = ref()
const detailRef = ref()

let tableColumns = reactive([
  { key: 'scoreRuleName', title: '规则名称',dataIndex: 'scoreRuleName' },
  { key: 'ruleKey', title: '规则key',dataIndex: 'ruleKey' },
  { key: 'overType', title: '过期规则',dataIndex: 'overType' },
  { key: 'agentNo', title: '机构号',dataIndex: 'agentNo' },
  { key: 'agentName', title: '机构名称',dataIndex: 'agentName' },
  { key: 'state', title: '状态',dataIndex: 'state' },
  { key: 'operation', title: '操作', dataIndex: 'operation', align: 'center' }
])

const searchData:any = ref({})

// 请求table接口数据
function reqTableDataFunc(params: any) {
  return $getScoreRule(params)
}

function searchFunc() {
  infoTableRef.value.refTable(true)
}

// 修改状态
function stateChangeFunc(scoreRuleId,state){
  const title = state === 1 ? '确认[启用]该规则？' : '确认[停用]该规则？'
  const content = state === 1 ? '启用后该用户将可领取该规则的福分' : '停用后该用户将无法领取该规则的福分'

  return new Promise<void>((resolve,reject) => {
    $infoBox.confirmDanger(title, content, () => {
      return $scoreRuleState({ scoreRuleId, state }).then(res => {
        infoTableRef.value.refTable(false)
        resolve()
      }).catch(err => reject(err))
    },
      () => {
      reject(new Error())
    })
  })
}
/**
 * 新增或修改
 */
function addOrEditFunc(scoreRuleId){
  addOrEditRef.value.show(scoreRuleId)
}

function detailFunc(scoreRuleId){  // 详情
  detailRef.value.show(scoreRuleId)
}
</script>
